﻿/// <reference path="jquery.min.js" />
/// <reference path="rsseasy.min.js" />
/// <reference path="rsseasy.app.js" />
/// <reference path="validated.app.js" />
/// <reference path="controller.js" />
/// <reference path="appconfig.js" />

        var mypage = new RssPage.Append("#mypage");
$(function () {
    RssApp.Menu.last().click(function (ev) {
        if (RssUser.IsLogin(ev)) {
            //定位
            JsAdapter.onLocationReceive = function (json) {
                RssApp["LocationData"] = json;
                RssORM.user_list.layout.myinfo();
            }
            JsAdapter.LocationListener({"once": true}).Submit();
        }
    });

    $("#mypage").load(function () {
        new Ajax("user/view").keyvalue("myid", RssUser.Data.myid).get(function (data) {
            RssUser.Update(data);
        });
    });

    //注册模块
    var regform = $("#regform");

    //----发送验证码
    var regtick = null;
    regform.find("h2").click(function () {
        var t = $(this);
        if (regvali.complete("regaccount")) {
            if (regtick) {
                return;
            }
            var count = 60;
            regtick = setInterval(function () {
                if (count-- <= 0) {
                    clearInterval(regtick);
                    regtick = null;
                    t.html("发送验证码");
                    return;
                }
                t.html(count + "秒后重新发送");
            }, 1000);

            new Ajax("regsms", {"account": regvali.rules.regaccount.value}).get(function (data) {
                RssApp["SmsCode"] = data;
            });
        }
    });

    //注册按钮
    $("[btnreg]").click(function () {
        var t = $(this);
        if (regvali.complete()) {
            t.prop("disabled", true);
            new Ajax("user/reg", {"account": regvali.rules.regaccount.value, "pwd": MD5(regvali.rules.regpwd.value), "realname": regvali.rules.realname.value}).get(function (data) {
                t.prop("disabled", false);
                if (data["rsscode"]) {
                    JsAdapter.ToastShow(dictdata.code[data["rsscode"]] || data["rsscode"]).Submit();
                    return;
                }
                data["account"] = regvali.rules.regaccount.value;
                data["nickname"] = regvali.rules.realname.value;
                data["avatar"] = 'avatar.png';

                RssUser.Update(data);

                JsAdapter.ToastShow("注册成功！").Submit();

                location.hash = "#mypage";
            });
        }
    });


    //登录模块
    $("[btnlogin]").click(function () {
        loginvalidate.onerror = function (key, msg) {
            JsAdapter.ToastShow(msg).Submit();
        }
        RssUser.Login.save($("#loginaccount").val(), $("#loginpwd").val());
        if (loginvalidate.complete()) {
            JsAdapter.Progress({"style": true, "message": "登录中"}).Submit();
            new Ajax("user/login", $("#loginform").serialize()).keyvalue("pwd", MD5(loginvalidate.rules.loginpwd.value)).getJson(function (data) {
                if (data['rsscode'] != null){
                    alert(dictdata.code[data['rsscode']]);
                    return;
                }
                RssUser.Update(data);
                RssUser.onLogin();
                JsAdapter.Progress({"action": "hide"}).Submit();
            });
        }
    });

    $("[btnupdatepwd]").click(function () {
        if (repwd.complete()) {
            JsAdapter.Progress({"style": true, "message": "修改中"}).Submit();

            new Ajax("user/resetpwd").keyvalue("oldpwd", MD5(repwd.rules.oldpwd.value)).keyvalue("pwd", MD5(repwd.rules.newpwd.value)).keyvalue("repwd", MD5(repwd.rules.renewpwd.value)).keyvalue("myid", RssUser.Data.myid).post(function (data) {
                JsAdapter.Progress({"action": "hide"}).Submit();
                if (data["rsscode"]) {
                    JsAdapter.ToastShow(dictdata.code[data["rsscode"]] || data["rsscode"]).Submit();
                    return;
                }
                JsAdapter.ToastShow("修改成功！").Submit();
                history.back();
            });
        }
    });

    //退出登录
    $("[btnloginout]").click(function () {
        RssUser.LoginOut();
        location.href = "index.html";
    });

    RssUser.SaveLogin = Storage.Get("SaveLogin");
    $("#jizumima").click(function () {
        RssUser.SaveLogin = this.checked;
        Storage.Set("SaveLogin", RssUser.SaveLogin);
    }).attr("checked", RssUser.SaveLogin);

    RssUser.Login.get();
    $("#loginaccount").val(RssUser.Login.account);
    $("#loginpwd").val(RssUser.Login.pwd);
});